﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace myDNS
{
    class Program
    {



        //// Display message delegate
        //private delegate void DisplayMessageDelegate(string message);
        //private DisplayMessageDelegate displayMessageDelegate = null;

        //#region Send And Receive

        //private void SendData(IAsyncResult ar,Socket clientSocket)
        //{
        //    try
        //    {

        //        clientSocket.EndSend(ar);
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine("Send Data: " + ex.Message, "UDP Client");
        //    }
        //}

        //private void ReceiveData(IAsyncResult ar, Socket clientSocket,byte[] dataStream)
        //{
        //    try
        //    {
        //        // Receive all data
        //        clientSocket.EndReceive(ar);

        //        // Initialise a packet object to store the received data

        //        String message = Provider.GetString(dataStream);

        //        // Reset data stream
        //        dataStream = new byte[1024];

        //        // Continue listening for broadcasts
        //        clientSocket.BeginReceiveFrom(dataStream, 0, dataStream.Length, SocketFlags.None, ref epServer, new AsyncCallback(this.ReceiveData), null);
        //    }
        //    catch (ObjectDisposedException)
        //    { }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine("Receive Data: " + ex.Message, "UDP Client");
        //    }
        //}

        //#endregion



        public static string clientSendToLocalDNS(DNSServer server, string hostName)
        {
            string kq = "";
            try
            {

                //Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);


                // Initialise server IP
                //IPAddress serverIP = IPAddress.Parse("127.0.0.1");

                // Initialise the IPEndPoint for the server and use port 107
                //IPEndPoint serverSend = new IPEndPoint(serverIP, 107);

                // Initialise the EndPoint for the server
                //EndPoint epServer = (EndPoint)serverSend;
                //byte[] byteSend = Provider.GetBytes(hostName);
                //sock.Send(byteSend);
                //byte[] result = new byte[1024];
                //sock.ReceiveFrom(result,result.Length,SocketFlags.None,ref epServer);



                UdpClient sock = new UdpClient();
                sock.Connect(server._ip, server._port);
                byte[] byteSend = Provider.GetBytes(hostName);
                sock.Send(byteSend, byteSend.Length);

                IPEndPoint ep = new IPEndPoint(0, 0);
                byte[] resultByteArray = sock.Receive(ref ep);
                kq = Provider.GetString(resultByteArray);
                sock.Close();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return kq;
        }

        //public static void UDP()
        //{

        //    // Client socket
        //    Socket clientSocket;
        //    // Server End Point
        //    EndPoint epServer;

        //    // Data stream
        //    byte[] dataStream = new byte[1024];

        //    try
        //    {
        //        do
        //        {
        //            Console.WriteLine("Type hostname ");
        //            Console.WriteLine("Ex : nslookup hostname");
        //            Console.Write("Type hostname : ");
        //            string inputText = Console.ReadLine();
        //            string[] inputTextArray = inputText.Split(new char[] { ' ' }, StringSplitOptions.None);
        //            if (inputTextArray[0].CompareTo("nslookup") == 0)
        //            {
        //                // Initialise socket
        //                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        //                // Initialise server IP
        //                IPAddress serverIP = IPAddress.Parse("127.0.0.1");

        //                // Initialise the IPEndPoint for the server and use port 107
        //                IPEndPoint server = new IPEndPoint(serverIP, 107);

        //                // Initialise the EndPoint for the server
        //                epServer = (EndPoint)server;

        //                string hostName = inputTextArray[1].ToString().ToLower();
        //                hostName = "0" + hostName;
        //                // Get packet as byte array
        //                byte[] data = Provider.GetBytes(hostName);

        //                // Send data to server
        //                clientSocket.BeginSendTo(data, 0, data.Length, SocketFlags.None, epServer, new AsyncCallback(this.SendData), null);

        //                // Initialise data stream
        //                dataStream = new byte[1024];



        //                // Begin listening for broadcasts
        //                clientSocket.BeginReceiveFrom(this.dataStream, 0, this.dataStream.Length, SocketFlags.None, ref epServer, new AsyncCallback(this.ReceiveData), null);


        //                //string kq = clientSendToLocalDNS(hcmiu_edu_vn_rootDNS, hostName);

        //                //Console.WriteLine(" IP: " + kq.ToString());
        //            }
        //            else
        //            {
        //                Console.WriteLine(" !!!! Wrong input format ");
        //            }
        //        }
        //        while (true);
        //    }
        //     catch (Exception ex)
        //    {
        //        Console.WriteLine("Connection Error: " + ex.Message);
        //    }    
        //}

        static void Main(string[] args)
        {




            do
            {
                Console.WriteLine("Type hostname ");
                Console.WriteLine("Ex : nslookup hostname");
                Console.Write("Type hostname : ");
                string inputText = Console.ReadLine();
                string[] inputTextArray = inputText.Split(new char[] { ' ' }, StringSplitOptions.None);
                if (inputTextArray[0].CompareTo("nslookup") == 0)
                {
                    string hostName = inputTextArray[1].ToString().ToLower();
                    hostName = "0" + hostName;
                    DNSServer hcmiu_edu_vn_rootDNS = new DNSServer("HCMIU.EDU.VN.ROOT DNS", "127.0.0.1", 107);
                    string kq = clientSendToLocalDNS(hcmiu_edu_vn_rootDNS, hostName);

                    Console.WriteLine(" IP: " + kq.ToString());
                }
                else
                {
                    Console.WriteLine(" !!!! Wrong input format ");
                }
            }
            while (true);

        }
    }
    
}
